package controller

import (
	"ci-control-backend/common/db"
	"ci-control-backend/common/httpx"
	"ci-control-backend/model"
	"ci-control-backend/tools"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
)

type UserController struct {
}

// Login 用户登录
func (*UserController) Login(ctx *httpx.Context) any {
	var req struct {
		Username string `json:"username"`
		Password string `json:"password"`
	}

	if err := ctx.ShouldBindJSON(&req); err != nil {
		return err
	}
	// 查询用户
	user, err := model.GetUserByUsername(req.Username)
	if err != nil || user == nil {
		// 找不到用户
		return errors.New("用户不存在")
	} else {
		// 验证用户密码
		if tools.ValidatePasswordAndHash(req.Password, user.Password) {
			// 验证通过，生成并保存user
			token, err := ctx.Token.Save(user, 0)
			if err != nil {
				// 保存失败，返回错误
				return err
			}
			// 保存成功，返回token
			return gin.H{"token": token}
		} else {
			// 验证不通过，返回错误
			return errors.New("用户名或密码错误")
		}
	}
}

// MyInfo 获取当前用户信息
func (*UserController) MyInfo(ctx *httpx.Context) any {
	userid, err := ctx.GetLoginUserid()
	if err != nil {
		return err
	}
	user, err := model.GetUserByID(userid)
	if err != nil {
		return err
	}
	user.Password = ""
	return user
}

// UpdateMyInfo 修改当前用户信息
func (*UserController) UpdateMyInfo(ctx *httpx.Context) any {
	var req struct {
		Email string `json:"email"`
	}

	if err := ctx.ShouldBindJSON(&req); err != nil {
		return err
	}

	userid, err := ctx.GetLoginUserid()
	if err != nil {
		return err
	}
	user, err := model.GetUserByID(userid)
	if err != nil {
		return err
	}
	updated := false
	if req.Email != "" {
		user.Email = req.Email
		updated = true
	}
	if updated {
		if _, err := user.Update(); err != nil {
			return err
		}
		return "修改成功"
	} else {
		return errors.New("参数错误")
	}
}

// UpdatePassword 修改当前用户密码
func (*UserController) UpdatePassword(ctx *httpx.Context) any {
	var req struct {
		Password    string `json:"password"`
		NewPassword string `json:"newPassword"`
		RePassword  string `json:"rePassword"`
	}

	if err := ctx.ShouldBindJSON(&req); err != nil {
		return err
	}
	if req.Password == "" || req.NewPassword == "" || req.RePassword == "" {
		return errors.New("参数错误")
	}
	if req.NewPassword != req.RePassword {
		return errors.New("两次输入密码不一致")
	}
	userid, err := ctx.GetLoginUserid()
	if err != nil {
		return err
	}
	user, err := model.GetUserByID(userid)
	if err != nil {
		return err
	}
	if !tools.ValidatePasswordAndHash(req.Password, user.Password) {
		return errors.New("密码错误")
	}
	if _, err := user.UpdatePassword(req.NewPassword); err != nil {
		return err
	}
	if err := ctx.Token.ClearOther(); err != nil {
		return err
	}
	return "修改成功"
}

// GetUserList 获取用户列表
func (*UserController) GetUserList(ctx *httpx.Context) any {
	var page, pageSize int
	err := ctx.MultiQuery(gin.H{
		"page":     &page,
		"pageSize": &pageSize,
	})
	if err != nil {
		return err
	}

	// 分页查询
	pageable := db.QueryPage(&model.User{})
	pageResult, err := pageable.PageParam(page, pageSize).Exec()
	if err != nil {
		return err
	}
	for _, user := range pageResult.PageData {
		user.Password = ""
	}
	return pageResult
}

// GetUserInfo 获取指定用户信息
func (*UserController) GetUserInfo(ctx *httpx.Context) any {
	var id uint
	err := ctx.Query("id", &id)
	if err != nil {
		return errors.New("参数错误")
	}
	user, err := model.GetUserByID(id)
	if err != nil {
		return err
	} else {
		user.Password = ""
		return user
	}
}
